package com.guaoran.interview.spring.autowired.controller;

import com.guaoran.interview.spring.autowired.service.AutowiredService;
import com.guaoran.interview.spring.autowired.service.QualifierService;
import com.guaoran.interview.spring.autowired.service.TransactionService;
import com.guaoran.interview.spring.scope.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author : 孤傲然
 * @description : {@link Autowired}
 * @see ResourceController
 * @desc : {@link Autowired} 可以声明在 字段，构造方法上， 默认根据类型进行注入，如果要根据名称进行注入，则需要指定{@link Qualifier}
 * @desc : {@link Autowired} 可以在多参数方法中， 但是 {@link Resource} 只能在单参数方法中
 * @date :2019/12/23 11:41
 */
@RestController
public class AutowiredController {
    @Autowired
    List<DemoService> demoServices;

    @Autowired
    Map<String,DemoService> demoServiceMap;

    @Autowired
    Map<String, AutowiredService> autowiredServiceMap;
    @Autowired
    List<AutowiredService> autowiredServiceList;

    @Autowired
    private AutowiredService[] autowiredServices;
    @Autowired
    private AutowiredService autowiredServiceImpl;
    @Autowired
    private AutowiredService autowiredServiceImpl2;
    @Autowired
    private AutowiredService autowiredServiceImpl3;


    private QualifierService qualifierService;
    private TransactionService transactionService;

//    // 使用 qualifier
//    @Autowired
//    @Qualifier("secondQualifierService")
//    private QualifierService qualifierService;
//    // 使用 primary 的 firstQualifierService
//    @Autowired
//    private QualifierService service;
//
//    @GetMapping("/qualifier")
//    public String qualifier(){
//        return qualifierService+"--"+service.toString();
//    }


//    public AutowiredController(QualifierService qualifierService,TransactionService transactionService){
//        System.out.println("有参数");
//        this.qualifierService = qualifierService;
//        this.transactionService = transactionService;
//    }
//    // 当通过构造方法注入时，如果未指定 @Autowired ,且存贮多个构造函数，则默认使用无参构造，如果也未指定无参构造，则报错
//    @Autowired
//    public AutowiredController(QualifierService qualifierService){
//        System.out.println("一个参数");
//        this.qualifierService = qualifierService;
//        this.transactionService = new TransactionService();
//    }


    @Autowired
    public void setParam(@Qualifier("secondQualifierService") QualifierService qualifierService,TransactionService transactionService){
        this.qualifierService = qualifierService;
        this.transactionService = transactionService;
    }
    @GetMapping("/autowired")
    public String autowired(){
        return qualifierService.toString()+"--"+transactionService.toString();
    }
}
